package com.example.android2019.ch5broadcast;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.android2019.R;

public class HelpActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn;
    MyReceiver2 myReceiver2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        btn = findViewById(R.id.ch5_help_btn);
        btn.setOnClickListener(this);
        myReceiver2 = new MyReceiver2();

        /*获取传递的数据*/
        Intent intent = this.getIntent();
        String a = intent.getStringExtra("test");
        Toast.makeText(this, a, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction("net.zjitc.help");
        registerReceiver(myReceiver2, filter);
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(myReceiver2);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setAction("net.zjitc.help");
        sendBroadcast(intent);
    }
}
